//system API wrapper
// $Id: sys_alloc.cpp 511 2010-07-15 11:06:32Z Oleg.Bulychov $

#include "stdafx.h"

#include "src/heo/include/mem_util.h"
#include "src/heo/include/sys_alloc.h"

#if defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#endif
//__________________________________________________________________________________
namespace mem_util
{
//__________________________________________________________________________________
//.............................................................................
sys_page_allocator::~sys_page_allocator() NO_THROW_()
{
    if (!p_)
        return;
#if defined(_WIN32)
    ::VirtualFree(p_, 0, MEM_RELEASE);
#else
    free(p_);
#endif
}
//.............................................................................
int sys_page_allocator::Alloc(size_t nPages) NO_THROW_()
{
    ASSERT_(!p_);
#if defined(_WIN32)
    p_ = ::VirtualAlloc(NULL, nPages * MEM_PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE);
    if (!p_)
        return 0;
    first_page_ = (TPage*)p_;
    ASSERT_(ROUND2(size_t(p_), MEM_PAGE_SIZE) == size_t(p_));
#else
    p_ = malloc((nPages + 1) * MEM_PAGE_SIZE);
    if (!p_)
        return 0;
    first_page_ = (TPage*)ROUND2(size_t(p_), MEM_PAGE_SIZE);
    ASSERT_((char*)first_page_ >= (char*)p_);
#endif
    num_pages_ = nPages;
    return 1;
}
//__________________________________________________________________________________
//__________________________________________________________________________________
}//:mem_util

//EOF!
